package demo.bocweb.login_module.activity;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;

import butterknife.BindView;
import butterknife.OnClick;
import demo.bocweb.com.sdk.baseApi.RequestHelper;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.MD5Utils;
import demo.bocweb.com.sdk.util.StringUtils;
import demo.bocweb.com.sdk.util.ToastyUtil;
import demo.bocweb.login_module.base.BaseFindPwdActivity;
import demo.bocweb.user_module.R;
import demo.bocweb.user_module.R2;
import okhttp3.RequestBody;

/**
 * 找回密码
 * Created by zhelun on 2018/1/26.
 */
@Route(path = ARoutConfig.findPwdPath)
public class FindPwdActivity extends BaseFindPwdActivity {
    @BindView(R2.id.phone)
    EditText phone;
    @BindView(R2.id.code)
    EditText code;
    @BindView(R2.id.send)
    TextView send;
    @BindView(R2.id.psw)
    EditText psw;
    @BindView(R2.id.eye)
    ImageView eye;
    @BindView(R2.id.psw_true)
    EditText pswTrue;
    @BindView(R2.id.eye_true)
    ImageView eyeTure;
    @BindView(R2.id.done)
    TextView done;
    private CountDownTimer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_find_pwd;
    }

    @Override
    public void findPwdSuccess() {
        ToastyUtil.showSuccess("已重置密码，请重新登录");
        finish();
    }

    @Override
    public void sendSmsCodeSuccess() {
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                send.setText(l / 1000 + "s");
                send.setClickable(false);
            }

            @Override
            public void onFinish() {
                send.setText("重新获取");
                send.setClickable(true);
            }
        }.start();
    }

    @Override
    public void checkSmsCodeSuccess() {
        findPwd();
    }

    @Override
    public void findPwd() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        if (StringUtils.isEmpty(code.getText().toString())) {
            ToastyUtil.showWarning("验证码不能为空");
            return;
        }
        if (StringUtils.isEmpty(psw.getText().toString())) {
            ToastyUtil.showWarning("密码不能为空");
            return;
        }
        if (StringUtils.isEmpty(pswTrue.getText().toString())) {
            ToastyUtil.showWarning("确认密码不能为空");
            return;
        }
        if (!pswTrue.getText().toString().trim().equals(psw.getText().toString().trim())) {
            ToastyUtil.showWarning("两次密码输入不一致");
            return;
        }
        Loading.show(this,getString(R.string.str_loading));
        RequestBody body = RequestHelper.build()
                .add("Phone", phone.getText().toString())
                .add("Password", MD5Utils.getMD5(pswTrue.getText().toString()))
                .add("SmsCode", code.getText().toString())
                .create();
        mPresenter.findPwd(body);
    }

    @Override
    public void sendSmsCode() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        RequestBody body = RequestHelper.build()
                .add("Phone", phone.getText().toString())
                .add("Type", 1)
                .create();
        mPresenter.sendSmsCode(body);
    }

    @Override
    public void checkSmsCode() {
        if (StringUtils.isEmpty(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("手机号不能为空");
            return;
        }
        if (!StringUtils.isPhoneNumberValid(phone.getText().toString().trim())) {
            ToastyUtil.showWarning("非法的手机号");
            return;
        }
        if (StringUtils.isEmpty(code.getText().toString())) {
            ToastyUtil.showWarning("验证码不能为空");
            return;
        }
        RequestBody body = RequestHelper.build()
                .add("Phone",phone.getText().toString())
                .add("Type", 1)
                .add("Sms", code.getText().toString())
                .create();
        mPresenter.checkSmsCode(body);
    }



    @OnClick({R2.id.send, R2.id.done})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.send) {
            sendSmsCode();
        } else if (i == R.id.done) {
            if (AppConfig.isNeedCheckSmsCode){
                checkSmsCode();
            }else{
                findPwd();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
